$(function () {
    init();
})

function init() {
    FastClick.attach(document.body);
    // var array = [];
    // getOptions(array, "", orgInfo.list);
    // $("#orgName").select({
    //     title: "选择机构",
    //     items: array
    // });
    // $("#orgName").attr("value", orgInfo.list[0].name);
    // $("#orgName").attr("data-values", orgInfo.list[0].id);
    //
    // $("#orgName").on("change", function () {
    //     loadWarehouse()
    //     beginSearch()
    // })
    loadWarehouse();
    $('body').on('submit', '#searchBar', function(event){
        //todo ...
        beginSearch()
    })
}

function getOptions(array, supername, list) {
    if (supername != "") supername = supername + "-";
    for (var i = 0; i < list.length; i++) {
        array.push({
            title: supername + list[i].name,
            value: list[i].id
        })
        if (list[i].childrenOrg.length > 0) {
            getOptions(array, supername + list[i].name, list[i].childrenOrg);
        }
    }
}

function loadWarehouse() {
    var organizationId = $("#orgName").attr("data-values");
    warehouseSelect = [];
    $("#warehouseInfo").after("<input style=\"margin:10px;width:calc( 100% - 40px);\"" +
        "                                                                  class=\"weui-input\" id=\"warehouseInfo\" type=\"text\"" +
        "                                                                  value=\"\" readonly=\"\" data-values=\"\">");
    $("#warehouseInfo").eq(0).remove();
    equipmemtService.warehouseInfo({organization: organizationId}, function (data) {
        if (data.success) {
            var serverData = data.datas;
            if (serverData.length == 0)
                $("#warehouseInfo").val("暂无仓库信息");
            else
                $("#warehouseInfo").val("全部仓库");
            var array = [{
                title:"全部仓库",
                value:"0"
            }]
            for (var i = 0; i < serverData.length; i++) {
                array.push({
                    title: serverData[i].name,
                    value: serverData[i].id
                })
            }
            warehouseSelect = array;
            $("#warehouseInfo").select({
                title: "选择仓库",
                items: array
            });
            $("#warehouseInfo").on("change", function () {
                beginSearch()
            })
            beginSearch();
        } else {
            $.toptip(data.errorMsg);
        }
    })
}

function beginSearch() {
    var organizationId = $("#orgName").attr("data-values");
    var searchKey=$("#searchInput").val();
    if(searchKey=="") searchKey=null;
    $("#page1").html("");
    var warehouseId=getWarehouseId();
    if(warehouseId==null) {
        var row = $(" <div class=\"row\" style=\"display: block;\">" +
            "                    <div class=\"flex\">" +
            "                        <div class=\"title\" style=\"flex: 1;\">无记录</div>" +
            "                    </div></div>");
        $("#page1").append(row);
        return;
    }
    equipmemtService.warehouseChangeList({
        type: "333101,333102,333103,333104",
        warehouseId: warehouseId,
        state: '334002',
        searchKey:searchKey
    }, function (data) {
        if (data.success) {
            var serverData = data.datas;
            if (serverData.length == 0) {
                var row = $(" <div class=\"row\" style=\"display: block;\">" +
                    "                    <div class=\"flex\">" +
                    "                        <div class=\"title\" style=\"flex: 1;\">无记录</div>" +
                    "                    </div></div>");
                $("#page1").append(row);
            } else {
               $(serverData).each(function(i,item){
                   var row = $("  <div class=\"row\" style=\"display: block;\">" +
                       "                    <div class=\"flex\">" +
                       "                        <div class=\"title\" style=\"flex: 1;\">"+item.eqClassName+"："+item.brandName+item.name+"</div>" +
                       "                        <div class=\"title\" style=\"width:50px; text-align: right\">"+item.count+"</div>" +
                       "                    </div>" +
                       "                    <div class=\"subTitle\">原因："+item.typeName+" "+item.reason+"</div>" +
                       "                    <div class=\"flex\">" +
                       "                        <div class=\"label\">出库日期</div>" +
                       "                        <div class=\"value\">"+item.updateTime+"</div>" +
                       "                    </div>" +
                       "                    <div class=\"flex\">" +
                       "                        <div class=\"label\">操作用户：</div>" +
                       "                        <div class=\"value\">"+item.createStaffName+"</div>" +
                       "                    </div>" +
                       "                    <div class=\"flex\">" +
                       "                        <div class=\"label\">出库单号：</div>" +
                       "                        <div class=\"value\">"+item.flowCode+"</div>" +
                       "                    </div>" +
                       "                </div>");
                   $("#page1").append(row);
               });
            }

        } else {
            $.toptip(data.errorMsg);
        }
    })
}

var warehouseSelect = [];

function getWarehouseId() {
    if ($("#warehouseInfo").attr("data-values") == ""||$("#warehouseInfo").attr("data-values") == "0") {
        if (warehouseSelect.length > 0) {
            var value = "";
            for (var i = 0; i < warehouseSelect.length; i++) {
                value += ("," + warehouseSelect[i].value);
            }
            return value.substr(1);
        } else return null;
    } else {
        return $("#warehouseInfo").attr("data-values");
    }
}